package com.ljm.boot.helloword.letCode;

import java.util.*;

public class Test49 {

    public static void main(String[] args) {
        String[] strs = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> lists = groupAnagrams(strs);
        System.out.println(lists.toString());
    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map  = new HashMap<String, List<String>>();
        List<List<String>> list2  = new ArrayList<List<String>>();

        for (int i=0;i< strs.length;i++) {
            String str = strs[i];
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String other  = String.valueOf(chars);
            //List<String> arr = map.getOrDefault(other,new ArrayList<String>());
            //list.add(str);
            //map.put(other,list);
            if(map.containsKey(other)){
                List<String> list  = map.get(other);
                list.add(str);
            }else{
                List<String> list  = new ArrayList<String>();
                list.add(str);
                map.put(other,list);
            }

        }

        for (String chars: map.keySet()){
            list2.add(map.get(chars));
        }
        return list2;
    }
}
